package com.xianrenit.xianrenpuzi.manager.interceptor;

import com.xianrenit.xianrenpuzi.manager.util.SpzxLoginAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//用来注册web组件：拦截器
@Configuration
public class SpzxWebMvcConfigurer implements WebMvcConfigurer {
    @Resource
    SpzxLoginAuthInterceptor spzxLoginAuthInterceptor;
    @Resource
    SpzxLoginAuthProperties spzxLoginAuthProperties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册登录认证的拦截器
        registry.addInterceptor(spzxLoginAuthInterceptor)
                .addPathPatterns("/**")//拦截路径
                .excludePathPatterns(spzxLoginAuthProperties.getWhiteUrls());//白名单路径

    }
    //配置跨域

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//异步访问哪些资源允许跨域访问
                .allowedHeaders("*") //允许携带请求头
                .allowedMethods("*")//允许所有请求方式
                .allowedOriginPatterns("*") //允许前端所有的服务器跨域访问
                .allowCredentials(true); //允许携带cookie
    }
}
